package com.seveneight.baijiesteward.ui.main.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
import com.alibaba.fastjson.JSON;
import com.github.jdsjlzx.interfaces.OnItemClickListener;
import com.seveneight.baijiesteward.R;
import com.seveneight.baijiesteward.adapter.PerfectRepaymentAdapter;
import com.seveneight.baijiesteward.base.BaseActivity;
import com.seveneight.baijiesteward.bean.AnyBean;
import com.seveneight.baijiesteward.bean.CardBean;
import com.seveneight.baijiesteward.bean.CreditBean;
import com.seveneight.baijiesteward.bean.CreditCardListBean;
import com.seveneight.baijiesteward.http.MyCallBack;
import com.seveneight.baijiesteward.ui.user.contract.CreditCardListContract;
import com.seveneight.baijiesteward.ui.user.contract.RemoveCardContract;
import com.seveneight.baijiesteward.ui.user.model.BindCreditCardUrlModel;
import com.seveneight.baijiesteward.ui.user.model.CreditCardListModel;
import com.seveneight.baijiesteward.ui.user.model.CreditCardModel;
import com.seveneight.baijiesteward.ui.user.model.RemoveCreditCardModel;
import com.seveneight.baijiesteward.ui.user.model.SettingDefaultCardModel;
import com.seveneight.baijiesteward.ui.user.presenter.RemoveCardPresenter;
import com.seveneight.baijiesteward.utils.WindowUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
//信用卡管理
public class PerfectRepaymentActivity extends BaseActivity implements View.OnClickListener,OnItemClickListener,RemoveCardContract.RemoveCarView{

    @BindView(R.id.tv_title)
    TextView tv_title;
    @BindView(R.id.repa_list_cade)
    ListView listView;
    private List<CreditBean> data = new ArrayList<>();
    PerfectRepaymentAdapter mAdapter;
    CreditCardListModel api=new CreditCardListModel();
    SettingDefaultCardModel apiSettingCard=new SettingDefaultCardModel();

    RemoveCreditCardModel removeCardPresenter=new RemoveCreditCardModel();
    BindCreditCardUrlModel bindCard=new BindCreditCardUrlModel();
    @Override
    protected void initLayout() {
        setContentView(R.layout.activity_perfect_repayment);
    }

    @Override
    protected void initView() {
        tv_title.setText("信用卡管理");
        View footer = LayoutInflater.from(this).inflate(R.layout.layout_cardlistbottom, null, false);
        mAdapter=new PerfectRepaymentAdapter(this,data);
        listView.setAdapter(mAdapter);
        listView.addFooterView(footer);
        mAdapter.notifyDataSetChanged();
        footer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivityIfNeeded(new Intent(getContext(),AddCreditCardActivity.class),111);
            }
        });
    }

    @Override
    protected void initData() {
        loadData();
    }
    public void loadData(){
        api.getCreditCardList(this, jsonFormat.commonParam(), new MyCallBack<List<CreditBean>>() {
            @Override
            public void onSuccess(List<CreditBean> creditBeans) {
                data=creditBeans;
                mAdapter.refresh(data);
//                mAdapter.notifyDataSetChanged();
            }

            @Override
            public void onFailed() {

            }
        });
    }
    int index=0;
    public void remove(final int index){
        this.index=index;
        new MaterialDialog.Builder(this)
                .content("是否移除该卡")
                .contentColorRes(R.color.graytext)
                .backgroundColorRes(R.color.white)
                .negativeColorRes(R.color.mainblue)
                .negativeText(R.string.negative)
                .positiveColorRes(R.color.mainblue)
                .positiveText(R.string.positive)
                .onPositive(new MaterialDialog.SingleButtonCallback() {
                    @Override
                    public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                        dialog.dismiss();
                        Map<String,String> params=new HashMap<>();
                        params.put("CreditCardID",data.get(index).getCreditCardID());
                        removeCardPresenter.getBankList(getContext(), jsonFormat.AddCard(params), new MyCallBack<AnyBean>() {
                            @Override
                            public void onSuccess(AnyBean anyBean) {
                                data.remove(index);
                                mAdapter.refresh(data);
                            }

                            @Override
                            public void onFailed() {

                            }
                        });
                    }
                })
                .build().show();
    }

    @Override
    protected void setListener() {

    }
    public void setdefaultCardDialog(String CreditCardID){
        Map<String,String> params=new HashMap<>();
        params.put("CreditCardID",CreditCardID);
        bindCard.getBankList(this, jsonFormat.AddCard(params), new MyCallBack<AnyBean>() {
            @Override
            public void onSuccess(AnyBean anyBean) {
                Bundle bundle5 = new Bundle();
                bundle5.putString("url", anyBean.getBindInfo());
                WindowUtil.getInstance().startActivity(getContext(), WebActivity.class, bundle5);
            }

            @Override
            public void onFailed() {

            }
        });



//        new MaterialDialog.Builder(this)
//                .content("是否设置为默认卡")
//                .contentColorRes(R.color.graytext)
//                .backgroundColorRes(R.color.white)
//                .negativeColorRes(R.color.mainblue)
//                .negativeText(R.string.negative)
//                .positiveColorRes(R.color.mainblue)
//                .positiveText(R.string.positive)
//                .onPositive(new MaterialDialog.SingleButtonCallback() {
//                    @Override
//                    public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
//                        dialog.dismiss();
//                        setApiSettingCard(CreditCardID);
//                    }
//                })
//                .build().show();
    }
    //设置默认卡
    public void setApiSettingCard(String CreditCardID){
        Map<String,String> params=new HashMap<>();
        params.put("CreditCardID",CreditCardID);
        apiSettingCard.getBankList(this, jsonFormat.AddCard(params), new MyCallBack<AnyBean>() {
            @Override
            public void onSuccess(AnyBean anyBean) {
                loadData();
            }

            @Override
            public void onFailed() {

            }
        });
    }

    @OnClick({R.id.iv_back})
    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.iv_back:
                finish();
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode==RESULT_OK){
            switch (requestCode){
                case 22:
                    int index=data.getIntExtra("index",1);
                    this.data.set(index, (CreditBean) data.getSerializableExtra("cardBean"));
                    mAdapter.refresh(this.data);
                    break;
                case 111:
                    loadData();
                    break;
            }
        }
    }

    @Override
    public void onItemClick(View view, int position) {
        if (true){
            startActivity(new Intent(this,AddCreditCardActivity.class));
        }
    }

    @Override
    public void reBackRemoveStatus() {
        data.remove(index);
        mAdapter.refresh(data);
    }
}
